package docker

// 在docker-110* docker-120* 完成的基础上
//redis集群-主从容错切换迁移案例

/*


	目录：
		1.数据读写存储
			1.1 启动6台容器并docker exec进入
			1.2 对 .11 新增两个key
			1.3 防止路由失效加入参数 -c 并新增两个key
			1.4 查看集群信息
					redis-cli --cluster check ip:端口号
					redis-cli --cluster check 宿主机ip/docker自定义网络分配的ip:端口号
		2.容错切换迁移



------------------1.数据读写存储------------------

-------1.1,1.2-------
	1.1,1.2 看图
		images/docker-122-redis集群-3主3从-哈希槽hash slot-03.png

	因为redis-cli 链接的单机
	因为 set k1 v1 ，算出来的hash值，大于第一个master[0]的最大槽位5460，所以存入不进去，报错


-------1.3-------


	改用 redis-cli -c 链接集群


root@3d185c900b30:/data# redis-cli -c
127.0.0.1:6379> cluster info
127.0.0.1:6379> cluster nodes
127.0.0.1:6379> set k1 v1
-> Redirected to slot [12706] located at 173.38.0.13:6379   ## [12706] 槽位，按照docker-120*里，集群时配置
OK
173.38.0.13:6379>   ###  此处已经跳转到 .13 的主机上了
173.38.0.13:6379> set k2 v2
-> Redirected to slot [449] located at 173.38.0.11:6379
OK
173.38.0.11:6379>	###  此处又跳转到 .11 的主机上了



## [12706] 槽位，按照docker-120*里，集群时配置，该条数据被存储于 .13 Master[2] 上

Master[0] -> Slots 0 - 5460								## 哈希槽  看图 images/docker-121-redis集群-3主3从-哈希槽hash slot-02.png
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383




-------1.4 查看集群信息------

		redis-cli --cluster check 宿主机ip/docker自定义网络分配的ip:端口号

	切换一个shell页面


root@VM-4-8-debian:~# docker exec -it redis-2 /bin/bash
root@a173e090b68a:/data# redis-cli --cluster check 173.38.0.11:6379
173.38.0.11:6379 (835210e0...) -> 1 keys | 5461 slots | 1 slaves.			#.11 有一个key
173.38.0.13:6379 (bc371156...) -> 1 keys | 5461 slots | 1 slaves.			#.13 有一个key
173.38.0.12:6379 (def95dae...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 2 keys in 3 masters.													##共有2个key在3个master下
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 173.38.0.11:6379)
M: 835210e0718b8cb17ec5174234642d83d315b3fa 173.38.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)													##添加了一个复制者/更随着
S: 4604d2508991c66160c6e0629b5d950f87b8b9f1 173.38.0.16:6379
   slots: (0 slots) slave
   replicates def95daeb6a1a0ac5c037a826f1d660cc82bd87e
S: 0e7f087ee4b8a5ea8ec4a38f0e724f20eb6b27bb 173.38.0.14:6379
   slots: (0 slots) slave
   replicates bc371156139e77e245427e278100744a497e3c0b
M: bc371156139e77e245427e278100744a497e3c0b 173.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 8ca526361a34855123de34902c49838fe1a5a46f 173.38.0.15:6379
   slots: (0 slots) slave
   replicates 835210e0718b8cb17ec5174234642d83d315b3fa
M: def95daeb6a1a0ac5c037a826f1d660cc82bd87e 173.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@a173e090b68a:/data#





------------------2.容错切换迁移------------------
	看图 images/docker-124-redis集群-主从切换.png


----2.1 停止 .11 一个master容器

	切换一个shell停止redis-1容器

root@VM-4-8-debian:~# docker stop redis-1
redis-1
root@VM-4-8-debian:~#

	在另一个shell窗口再次进入redis集群

root@a173e090b68a:/data# redis-cli -c
127.0.0.1:6379> cluster nodes
4604d2508991c66160c6e0629b5d950f87b8b9f1 173.38.0.16:6379@16379 slave def95daeb6a1a0ac5c037a826f1d660cc82bd87e 0 1665303806523 2 connected
835210e0718b8cb17ec5174234642d83d315b3fa 173.38.0.11:6379@16379 master,fail - 1665303743506 1665303741000 1 connected
0e7f087ee4b8a5ea8ec4a38f0e724f20eb6b27bb 173.38.0.14:6379@16379 slave bc371156139e77e245427e278100744a497e3c0b 0 1665303806000 3 connected
8ca526361a34855123de34902c49838fe1a5a46f 173.38.0.15:6379@16379 master - 0 1665303806924 7 connected 0-5460
def95daeb6a1a0ac5c037a826f1d660cc82bd87e 173.38.0.12:6379@16379 myself,master - 0 1665303805000 2 connected 5461-10922
bc371156139e77e245427e278100744a497e3c0b 173.38.0.13:6379@16379 master - 0 1665303805000 3 connected 10923-16383
127.0.0.1:6379>

	.11:6379@16379 master,fail  .11已经停止
	.15:6379@16379 master		.15 ，在docker-120*中已经发现是.11的从机，现在已经变成了主机



127.0.0.1:6379> get k1
-> Redirected to slot [12706] located at 173.38.0.13:6379
"v1"
173.38.0.13:6379> get k2
-> Redirected to slot [449] located at 173.38.0.15:6379     ###这里，上面set时，显示的是.11存入，现在成功从.15获取
"v2"
173.38.0.15:6379>




----2.2 重启 .11/redis-1容器


root@VM-4-8-debian:~# docker start redis-1
redis-1
root@VM-4-8-debian:~#

	在集群中查看

173.38.0.15:6379> cluster nodes
4604d2508991c66160c6e0629b5d950f87b8b9f1 173.38.0.16:6379@16379 slave def95daeb6a1a0ac5c037a826f1d660cc82bd87e 0 1665304288765 2 connected
8ca526361a34855123de34902c49838fe1a5a46f 173.38.0.15:6379@16379 myself,master - 0 1665304288000 7 connected 0-5460
835210e0718b8cb17ec5174234642d83d315b3fa 173.38.0.11:6379@16379 slave 8ca526361a34855123de34902c49838fe1a5a46f 0 1665304288000 7 connected
0e7f087ee4b8a5ea8ec4a38f0e724f20eb6b27bb 173.38.0.14:6379@16379 slave bc371156139e77e245427e278100744a497e3c0b 0 1665304288000 3 connected
def95daeb6a1a0ac5c037a826f1d660cc82bd87e 173.38.0.12:6379@16379 master - 0 1665304288000 2 connected 5461-10922
bc371156139e77e245427e278100744a497e3c0b 173.38.0.13:6379@16379 master - 0 1665304287000 3 connected 10923-16383
173.38.0.15:6379>



8ca526361a34855123de34902c49838fe1a5a46f 173.38.0.15:6379@16379 myself,master - 0 1665304288000 7 connected 0-5460
										 173.38.0.11:6379@16379 slave 8ca526361a34855123de34902c49838fe1a5a46f 0 1665304288000 7 connected


8ca526361 173.38.0.15:6379@16379 myself,master
		   173.38.0.11:6379@16379 slave 8ca526361


明确看出 .15是主机  .11 作为了.15的从机



	如果想恢复以前的关系，此处再把 .15 先停后启即可



root@VM-4-8-debian:~# docker stop redis-5
redis-5
root@VM-4-8-debian:~# docker start redis-5
redis-5
root@VM-4-8-debian:~#



173.38.0.15:6379> cluster nodes
8ca526361a34855123de34902c49838fe1a5a46f 173.38.0.15:6379@16379 myself,slave 835210e0718b8cb17ec5174234642d83d315b3fa 0 1665304761000 8 connected
835210e0718b8cb17ec5174234642d83d315b3fa 173.38.0.1 master - 0 1665304763269 8 connected 0-5460
0e7f087ee4b8a5ea8ec4a38f0e724f20eb6b27bb 173.38.0.14:6379@16379 slave bc371156139e77e245427e278100744a497e3c0b 0 1665304762266 3 connected
bc371156139e77e245427e278100744a497e3c0b 173.38.0.13:6379@16379 master - 0 1665304762000 3 connected 10923-16383
4604d2508991c66160c6e0629b5d950f87b8b9f1 173.38.0.16:6379@16379 slave def95daeb6a1a0ac5c037a826f1d660cc82bd87e 0 1665304762567 2 connected
def95daeb6a1a0ac5c037a826f1d660cc82bd87e 173.38.0.12:6379@16379 master - 0 1665304761000 2 connected 5461-10922
173.38.0.15:6379>


	上面可得  .11 又成为了 .15 的主机


173.38.0.15:6379> get k1
-> Redirected to slot [12706] located at 173.38.0.13:6379
"v1"
173.38.0.13:6379> get k2
-> Redirected to slot [449] located at 173.38.0.11:6379    ##又从.11读取
"v2"
173.38.0.11:6379>


	查看集群信息，信息一致

root@a173e090b68a:/data# redis-cli --cluster check 173.38.0.11:6379



root@a173e090b68a:/data# redis-cli --cluster check 173.38.0.11:6379
173.38.0.11:6379 (835210e0...) -> 1 keys | 5461 slots | 1 slaves.
173.38.0.12:6379 (def95dae...) -> 0 keys | 5462 slots | 1 slaves.
173.38.0.13:6379 (bc371156...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 2 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 173.38.0.11:6379)
M: 835210e0718b8cb17ec5174234642d83d315b3fa 173.38.0.11:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: def95daeb6a1a0ac5c037a826f1d660cc82bd87e 173.38.0.12:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 4604d2508991c66160c6e0629b5d950f87b8b9f1 173.38.0.16:6379
   slots: (0 slots) slave
   replicates def95daeb6a1a0ac5c037a826f1d660cc82bd87e
S: 0e7f087ee4b8a5ea8ec4a38f0e724f20eb6b27bb 173.38.0.14:6379
   slots: (0 slots) slave
   replicates bc371156139e77e245427e278100744a497e3c0b
M: bc371156139e77e245427e278100744a497e3c0b 173.38.0.13:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 8ca526361a34855123de34902c49838fe1a5a46f 173.38.0.15:6379
   slots: (0 slots) slave
   replicates 835210e0718b8cb17ec5174234642d83d315b3fa
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@a173e090b68a:/data#



*/
